﻿using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace UFrame
{
    public static class PLUIMenu
    {
        [MenuItem("Plateface/UGUI/ConfigWindow", false, 1)]
        public static void OpenUGUIConfigWindow()
        {
            PLUIGlobal win = EditorWindow.GetWindow<PLUIGlobal>(false, "UGUIConfig", false);
            win.Show();
        }

        public static string uiDirPath = "bawAsset/Prefabs/UI/";
        public static string classPath = "Script/UIDefine.cs";

        private static string classTemp =
            @"
        using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;
        using UnityEngine.UI;

        namespace UFrame
        {
            public partial class UIRegisterCode
            {
        
                public void InitUI()
                {
                    //@Init
                    
                }

                public static void InitUIInfo()
                {
                    //@Init
                    
                }
            }

            
        }

            public struct EUI
            {
                   //@UIField
            }

        ";

        [MenuItem("Plateface/UGUI/CreateCode")]
        public static void CreateCode()
        {
            LoadPath();
            string path = Path.Combine(Application.dataPath, uiDirPath);
            Debug.Log("生成代码" + path);

            //  UIMa.AddUIInfo(EUI.MainUI, @"Assets/bawAsset/Prefabs/UI/MainUI.prefab");   注册
            StringBuilder sb = new StringBuilder();
            StringBuilder eui = new StringBuilder();
            GameObject go = null;
            var fileList = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);
            int index = 0;

            string s = "Resources";
            foreach (var filePath in fileList)
            {
                string newPath = string.Empty;
                newPath = filePath.Replace("\\", "/");

                newPath = newPath.Substring(newPath.IndexOf("Assets"));

                //Debug.Log(newPath);

                if (newPath.Contains(s))
                {
                    newPath = newPath.Substring(newPath.IndexOf(s) + s.Length + 1);
                    newPath = newPath.Substring(0, newPath.IndexOf("."));
                    go = Resources.Load<GameObject>(newPath);
                }
                else
                {
                    newPath = newPath.Substring(0, newPath.IndexOf("."));
                    go = AssetDatabase.LoadAssetAtPath<GameObject>(newPath) as GameObject;
                }

                if (go == null)
                {
                    Debug.LogError(newPath);
                    continue;
                }
                if (go.GetComponent<BaseUI>())
                {
                    sb.AppendLine($"UIMa.AddUIInfo(EUI.{go.name}, @\"{newPath}\");");
                    eui.AppendLine($"public static int {go.name} = {index};");
                    index++;
                }
            }

            string codeClass = classTemp.Replace("//@Init", sb.ToString());
            codeClass = codeClass.Replace("//@UIField", eui.ToString());

            path = Path.Combine(Application.dataPath, classPath);
            File.WriteAllText(path, codeClass, Encoding.Unicode);
            Debug.Log(codeClass);
            AssetDatabase.Refresh();
        }

        private static void LoadPath()
        {
            //Debug.Log("加载配置....");
            TextAsset t = Resources.Load<TextAsset>("UFrameAssetEditor");
            if (t == null)
            {
                return;
            }

            var lines = t.text.Split('\n');

            string path = string.Empty;

            path = StringTool.LineByKeyPair("UIDefineClass", lines);
            if (!string.IsNullOrEmpty(path))
            {
                classPath = path.Trim();
            }

            path = StringTool.LineByKeyPair("UIDefinePrefabs", lines);
            if (!string.IsNullOrEmpty(path))
            {
                uiDirPath = path.Trim();
            }
        }
    }
}